package scau.hotel.hotel_sys.service.Impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import scau.hotel.hotel_model.controller.RespBean;
import scau.hotel.hotel_model.controller.RespBeanEnum;
import scau.hotel.hotel_sys.entity.SysUser;
import scau.hotel.hotel_sys.mapper.SysUserMapper;
import scau.hotel.hotel_sys.security.utils.SecurityUtils;
import scau.hotel.hotel_sys.service.ISysUserService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * (SysUser)表服务实现类
 *
 * @author makejava
 * @since 2022-04-20 15:42:44
 */
@Service("sysUserService")
public class ISysUserServiceImpl implements ISysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 通过用户姓名查询用户对象
     *
     * @param username 主键
     * @return 实例对象
     */
    @Override
    public SysUser queryByUsername(String username){
        return this.sysUserMapper.queryByUsername(username);
    };

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysUser queryById(Long id) {
        return this.sysUserMapper.queryById(id);
    }

    /**
     * 查询所有
     *
     * @param sysUser 筛选条件
     * @return 查询结果
     */
    @Override
    public List<SysUser> queryAll(SysUser sysUser) {
        List<SysUser> sysUserList = this.sysUserMapper.queryAll(sysUser);
        //不显示root账号
        sysUserList.remove(0);
        return sysUserList;
    }

    /**
     * 分页查询
     *
     * @param sysUser 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<SysUser> queryByPage(SysUser sysUser, PageRequest pageRequest) {
        long total = this.sysUserMapper.count(sysUser);
        return new PageImpl<>(this.sysUserMapper.queryAllByLimit(sysUser, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public RespBean insert(SysUser sysUser) {
        //判断账号名是否重复
        if (this.sysUserMapper.queryByUsername(sysUser.getUsername())!=null){
            return RespBean.error(RespBeanEnum.CONFLICT,"账号名已存在！");
        }else {
            //记录创建时间
            sysUser.setCreateTime(new Date());
            //默认状态
            sysUser.setStatus(1);
            // 使用BCrypt加密密码
            String password = new BCryptPasswordEncoder().encode(sysUser.getPassword());
            //修改成加密密码
            sysUser.setPassword(password);
            //插入数据库
            this.sysUserMapper.insert(sysUser);
            return RespBean.success("添加成功！");
        }
    }

    /**
     * 修改数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public RespBean update(SysUser sysUser) {
        //判断账号名是否重复
        if (this.sysUserMapper.queryByUsername(sysUser.getUsername())!=null && sysUser.getPassword() == null){
            return RespBean.error(RespBeanEnum.CONFLICT,"账号名已存在！");
        }
        //判断是否更改密码
        if (sysUser.getPassword() != null){
            if(SecurityUtils.matchesPassword(sysUser.getPassword(),this.sysUserMapper.queryByUsername(sysUser.getUsername()).getPassword())){
                return RespBean.error(RespBeanEnum.CONFLICT,"新密码不能与旧密码相同！");
            }else {
                //记录修改时间
                sysUser.setUpdateTime(new Date());
                this.sysUserMapper.update(sysUser);
                return RespBean.success("修改成功！",this.queryById(sysUser.getId()));
            }
        }
        else {
            //记录修改时间
            sysUser.setUpdateTime(new Date());
            this.sysUserMapper.update(sysUser);
            return RespBean.success("修改成功！",this.queryById(sysUser.getId()));
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public RespBean deleteById(Long id) {
        SysUser user = this.sysUserMapper.queryById(id);
        //root账号无法删除
        if (Objects.equals(user.getUsername(), "root") && user.getId() == 1){
            return RespBean.error(RespBeanEnum.UNAUTHORIZED_ERROR,"该账号无法删除！");
        }
        this.sysUserMapper.deleteById(id);
        return RespBean.success("删除成功");
    }
}
